package boardgamer.yavalath.client.preference;

import android.content.SharedPreferences;
import boardgamer.common.client.preference.RegisteredEnumIconList;
import boardgamer.common.client.preference.RegisteredEnumList;
import boardgamer.common.client.preference.StringConverter;
import boardgamer.yavalath.R;

import static boardgamer.yavalath.client.Settings.KEY_COLOR_PLAYER_PREFIX;
import static boardgamer.yavalath.common.YavalathPlayerState.PlayerColor;

/**
 * @author garysoed
 */
public class RegisteredPlayerColorPicker extends RegisteredEnumIconList<PlayerColor> {

  public static final StringConverter<PlayerColor> PLAYER_COLOR_CONVERTER
      = new StringConverter<PlayerColor>() {

    @Override public String toString(PlayerColor value) {
      return value.name();
    }

    @Override public PlayerColor valueOf(String stringValue) {
      return PlayerColor.valueOf(stringValue);
    }
  };

  public static final RegisteredEnumList.Renderer<PlayerColor> PLAYER_COLOR_RENDERER
      = new RegisteredEnumList.Renderer<PlayerColor>() {
    @Override
    public String render(PlayerColor object) {
      return object.getRenderedName();
    }
  };

  public static final RegisteredEnumIconList.IconRenderer<PlayerColor> PLAYER_COLOR_ICON_RENDERER
      = new RegisteredEnumIconList.IconRenderer<PlayerColor>() {
    @Override
    public int getImageResource(PlayerColor value) {
      switch (value) {
        case RED:
          return R.drawable.settings_color_red;
        case ORANGE:
          return R.drawable.settings_color_orange;
        case YELLOW:
          return R.drawable.settings_color_yellow;
        case CHARTREUSE:
          return R.drawable.settings_color_chartreuse;
        case AQUA:
          return R.drawable.settings_color_aqua;
        case BLUE:
          return R.drawable.settings_color_blue;
        case MAGENTA:
          return R.drawable.settings_color_magenta;
        case BROWN:
          return R.drawable.settings_color_brown;
        case WHITE:
          return R.drawable.settings_color_white;
        default:
          throw new UnsupportedOperationException("Unimplemented");
      }
    }
  };
  
  public static final PlayerColor[] DEFAULT_PLAYER_COLOR = new PlayerColor[] {
    PlayerColor.RED,
    PlayerColor.BLUE,
    PlayerColor.CHARTREUSE
  };
  
  public RegisteredPlayerColorPicker(int playerId, PlayerColorPickerPreference preference,
      SharedPreferences sharedPreferences) {
    super(KEY_COLOR_PLAYER_PREFIX + "-" + playerId,
        preference,
        DEFAULT_PLAYER_COLOR[playerId],
        PlayerColor.class,
        sharedPreferences,
        PLAYER_COLOR_CONVERTER,
        PLAYER_COLOR_RENDERER,
        PLAYER_COLOR_ICON_RENDERER);
    
    preference.setPlayerId(playerId);
    preference.setSharedPreferences(sharedPreferences);
  }
}
